package com.hurui.xiaoming.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.bean.Li_ContractResult;

import java.util.List;

/**
 * Created by hurui on 2017/7/16.
 */

public class ContarctAdapter extends RecyclerView.Adapter<ContarctAdapter.DetailedListHolder> {
    private Context mContext;
    private List<Li_ContractResult.DataBean> lists;

    private static OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件

    public interface OnItemClickListener {
        void onItemJieKuanClick(View view,Li_ContractResult.DataBean dataBean); //设置点击事件
        void onItemJuJianClick(View view,Li_ContractResult.DataBean dataBean); //设置点击事件
        void onItemShouJuClick(View view,Li_ContractResult.DataBean dataBean); //设置点击事件
    }
    public static void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public ContarctAdapter(Context mContext, List<Li_ContractResult.DataBean> lists) {
        this.mContext = mContext;
        this.lists = lists;
    }


    public void refreshData(List<Li_ContractResult.DataBean> data) {
        lists = data;
        this.notifyDataSetChanged();

    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(lists.get(position).getOi_id());
    }

    @Override
    public DetailedListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.contract_list_item, parent, false);
        return new DetailedListHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final DetailedListHolder holder, final int position) {
        final Li_ContractResult.DataBean dataBean = lists.get(position);
        //需要修改
        holder.order_id.setText("订单编号" + dataBean.getOi_id());
                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期");
                holder.order_money_tv.setText(dataBean.getOi_jkprice());
                if (dataBean.getOi_addtime()!=null)
                holder.order_time_tv.setText("借款日期: " + dataBean.getHktime());

        if (mOnItemClickListener != null) {
            holder.jiekuan_contract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // todo 借款合同
                    if("".equals(dataBean.getOi_htid2())){
                        Toast.makeText(mContext,"合同未生成",Toast.LENGTH_SHORT).show();
                    }else{
                        mOnItemClickListener.onItemJieKuanClick(((DetailedListHolder)holder).jiekuan_contract, dataBean);
                    }
                }
            });
            holder.jujian_contract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // todo 居间合同
                    if("".equals(dataBean.getOi_htid1())){
                        Toast.makeText(mContext,"合同未生成",Toast.LENGTH_SHORT).show();
                    }else{
                        mOnItemClickListener.onItemJuJianClick(((DetailedListHolder)holder).jiekuan_contract, dataBean);
                    }
                }
            });
            holder.shouju_contract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // todo 收据合同
                    if("".equals(dataBean.getOi_htid3())){
                        Toast.makeText(mContext,"合同未生成",Toast.LENGTH_SHORT).show();
                    }else{
                        mOnItemClickListener.onItemShouJuClick(((DetailedListHolder)holder).jiekuan_contract, dataBean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class DetailedListHolder extends RecyclerView.ViewHolder {
        private TextView order_info_tv;//借款金额
        private TextView order_money_tv;
        private TextView order_time_tv;
        private TextView order_id;
        private TextView jiekuan_contract,jujian_contract,shouju_contract;


        public DetailedListHolder(View itemView) {
            super(itemView);
            order_info_tv = (TextView) itemView.findViewById(R.id.order_info_tv);
            order_money_tv = (TextView) itemView.findViewById(R.id.order_money_tv);
            order_time_tv = (TextView) itemView.findViewById(R.id.order_time_tv);
            order_id = (TextView) itemView.findViewById(R.id.order_id);
            jiekuan_contract = (TextView) itemView.findViewById(R.id.jiekuan_contract);
            jujian_contract = (TextView) itemView.findViewById(R.id.jujian_contract);
            shouju_contract = (TextView) itemView.findViewById(R.id.shouju_contract);
        }
    }
}
